package com.zcpt.controller.projects;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zcpt.order.IAddressService;
import com.zcpt.pojo.order.Address;
import com.zcpt.pojo.projects.Project;
import com.zcpt.pojo.projects.Reward;
import com.zcpt.pojo.users.User;
import com.zcpt.projects.IProjectService;
import com.zcpt.projects.IRewardService;
import com.zcpt.realname.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Api(tags = "项目信息管理接口")
@Controller
@RequestMapping("/projects")
public class ProjectController {
    @Reference
    private IProjectService projectService;

    @Reference
    private IAddressService addressService;

    @Reference
    private IRewardService rewardService;

    @GetMapping("/projectQueryAll")
    public String projectListAll(Model model, @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        IPage<Project> projectPage = projectService.projectListByCondition(pageNum,pageSize, null);
        model.addAttribute("totalnum", projectPage.getTotal());
        model.addAttribute("totalpage",projectPage.getPages());
        model.addAttribute("projectList", projectPage.getRecords());
        return "overview";
    }
    // 项目总览：按需查询
    @PostMapping("/projectQuery")
    @ApiOperation(value = "条件分页查询项目列表",notes = "根据条件查询项目列表信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(value = "当前页数",name = "pagenum",required = false,dataType = "Integer",paramType = "query"),
//            @ApiImplicitParam(value = "每页条数",name = "pagesize",required = false,dataType = "Integer",paramType = "query"),
//            @ApiImplicitParam(value = "查询条件",name = "queryMap",required = false,dataType = "Map",paramType = "body")
//    })
//    public String projectListQuery(Model model, @RequestBody Map<String,Object> queryMap, HttpServletResponse response){
    public String projectListQuery(Model model, @RequestParam Integer pageNum, @RequestParam Integer pageSize,@RequestParam String category,HttpServletResponse response){
        System.out.println("请求成功------------");
        System.out.println("pageNum:"+pageNum);
        System.out.println("pageSize:"+pageSize);
        System.out.println("category:"+category);
//        System.out.println("请求成功------------");
//        response.setContentType("application/json");
        Map queryMap = new HashMap();
        queryMap.put("category", category);
        IPage<Project> projectPage = projectService.projectListByCondition(pageNum,pageSize, queryMap);
        model.addAttribute("totalnum", projectPage.getTotal());
        model.addAttribute("totalpage",projectPage.getPages());
        model.addAttribute("projectList", projectPage.getRecords());
        return "overview";
    }

    //首页部分项目展示
    @GetMapping("/projectQuerySome")
    @ApiOperation(value = "首页部分项目列表",notes = "根据条件查询项目列表信息")
    @ApiImplicitParam(value = "查询条件",name = "queryMap",required = false,dataType = "Map",paramType = "query")
    public String projectListSome(Model model){
        Map<String,Object> queryMap1 = new HashMap<>();
        Map<String,Object> queryMap2 = new HashMap<>();
        Map<String,Object> queryMap3 = new HashMap<>();
        Map<String,Object> queryMap4 = new HashMap<>();

        queryMap1.put("category", "科技");
        queryMap2.put("category", "设计");
        queryMap3.put("category", "农业");
        queryMap4.put("category", "其他");

        IPage<Project> projectPage1 = projectService.projectListByCondition(1, 4, queryMap1);
        IPage<Project> projectPage2 = projectService.projectListByCondition(1, 4, queryMap2);
        IPage<Project> projectPage3 = projectService.projectListByCondition(1, 4, queryMap3);
        IPage<Project> projectPage4 = projectService.projectListByCondition(1, 4, queryMap4);

        model.addAttribute("projectList1", projectPage1.getRecords());
        model.addAttribute("projectList2", projectPage2.getRecords());
        model.addAttribute("projectList3", projectPage3.getRecords());
        model.addAttribute("projectList4", projectPage4.getRecords());
        return "index";
    }

    //项目详情展示：通过id查找项目
    @GetMapping("/projectQueryOne")
    public String projectOne(Model model,Integer id){
        Project project = projectService.findProById(id);
        model.addAttribute("project", project);
        return "item";
    }

    @GetMapping("/queryAll")
    public String queryAll(Model model,Integer project_id){
        Map<String, Object> map = projectService.findProjectAndReward(project_id);
        List<Address> addressList = addressService.findByUserId(1);
        System.out.println(map.get("project"));
        System.out.println(map.get("reward"));
        model.addAttribute("addressList", addressList);
        model.addAttribute("project", map.get("project"));
        model.addAttribute("reward", map.get("reward"));
        return "pay-address";
    }

    @PostMapping("/insertPro")
    public String insertPro(Model model , MultipartFile[] projectinfo_img1, Project project) throws IOException {
        List<String> pathArr = new ArrayList<>();

//        图片的上传
        for (MultipartFile mf : projectinfo_img1) {
            // 获取文件名
            String originalFilename = mf.getOriginalFilename();
            // 设置图片上传路径
            String url = "/opt/img";
            // 以绝对路径保存重名命后的图片
            mf.transferTo(new File(url + "/" + originalFilename));
            // 把图片存储路径保存到数据库
            String path = url + originalFilename;
            pathArr.add(path);
        }
        project.setNumberCollect(0);
        project.setNumberSupport(0);
        project.setMoneyHaved(0.0);
//        创建日期
        project.setCreated(new Date());
//        已筹天数
        project.setDaysHaved(0);
        project.setImgHead(pathArr.get(0));
        project.setImgDetails(pathArr.get(1));
        project.setStatus("0");
        project.setIs_pass("0");
        System.out.println(project);
        Integer integer = projectService.insertPro(project);

        Project pro = projectService.findProByName(project.getName());
        Integer proId = pro.getId();
        model.addAttribute("proId" , proId);
        return "return for report";
    }

    @PostMapping("/myReword")
    public String addReword(MultipartFile return_img , HttpServletRequest request) throws IOException {
        Reward reward = new Reward();

        String originalFilename = return_img.getOriginalFilename();
        File file = new File("/opt/img");
        return_img.transferTo(new File(file , originalFilename));
        reward.setImage(file.getPath()+"/"+originalFilename);

        reward.setCategory(request.getParameter("inlineRadioOptions"));
        reward.setMoney(Double.parseDouble(request.getParameter("return_money")));
        reward.setContent(request.getParameter("return_info"));
        reward.setAmountTotal(Integer.parseInt(request.getParameter("return_amount")));
        reward.setAmountOne(Integer.parseInt(request.getParameter("inlineRadioOptions2")));
        reward.setFare(Double.parseDouble(request.getParameter("return_fare")));
        reward.setIsInvoice(request.getParameter("inlineRadioOptions1"));
        reward.setTimeLen(Integer.parseInt(request.getParameter("return_date")));
        reward.setId(Integer.parseInt(request.getParameter("id")));
        Integer integer = rewardService.insertReword(reward);

        return "launch crowdfunding complete";
    }
}
